package top.lcm889.juc.原子类;

import top.lcm889.juc.count_down_latch.CustomerThread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Coder-lcm
 * @date 2021/4/30
 */
public class AtomicTest {
    private AtomicInteger cnt = new AtomicInteger();

    public void add(){
        System.out.println(Thread.currentThread().getName() + " " +  cnt);
        cnt.incrementAndGet();
    }

    public int get(){
        return cnt.get();
    }

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        final int threadCount = 1000000;
        AtomicTest atomicTest = new AtomicTest();
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        TimeUnit unit;
        BlockingQueue workQueue = new LinkedBlockingQueue<CustomerThread>();

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(9, 30,30 , TimeUnit.SECONDS, workQueue);
//      ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < threadCount; i++) {
            threadPoolExecutor.execute(() -> {
                atomicTest.add();
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        threadPoolExecutor.shutdown();
        System.out.println(atomicTest.get());
        System.out.println("waste time : " + (System.currentTimeMillis() - start));
    }
}
